package club.janna.netty.recycle;

import io.netty.util.Recycler;

import java.util.Arrays;

public class RecycleDemo {

    static Recycler<User> recycler = new Recycler<User>() {
        @Override
        protected User newObject(Handle<User> handle) {
            return new User(handle);
        }
    };

    public static void main(String[] args) {
        final int N = 20;

        User[] users = new User[N];
        for (int i = 0; i < N; i++) {
            users[i] = recycler.get();
        }

        for (int i = 0; i < N; i++) {
            users[i].recycle();
        }
        System.out.println("users -> " + Arrays.toString(users));
    }

    private static class User {
        Recycler.Handle<User> handle;

        public User(Recycler.Handle<User> handle) {
            this.handle = handle;
        }

        public void recycle() {
            handle.recycle(this);
        }
    }
}
